#include "symmetricKernConvolution.h"

void simpleSymmetricKernConv(Mat_<uchar>& inImg, Mat_<float>& kern, Mat_<uchar>& outImg){

	// Kernel MUST be squared
    if(kern.cols != kern.rows) return;

	int img_rows = inImg.rows;
	int img_cols = inImg.cols;

    // Calculate half dim of kernel matrix (integer division)
    int k_ap_sz = kern.cols/2;

	for(int r=k_ap_sz; r<img_rows-k_ap_sz; ++r){
		for(int c=k_ap_sz; c<img_cols-k_ap_sz; ++c){

			float sum = inImg(r,c)*kern(k_ap_sz, k_ap_sz);

			for(int kr=0; kr<k_ap_sz+1; ++kr){
                for(int kc=0; kc<k_ap_sz; ++kc){
					
					// Define coordinates of the 4 equal kernel elements
					int uprow = r-k_ap_sz+kr;
					int downrow = r+k_ap_sz-kr;
					int leftcol = c-k_ap_sz+kc;
					int rightcol = c+k_ap_sz-kc;

					// Multiply 4 image pxl with the same kernel element
					sum += kern(kr,kc) * ( inImg(uprow, leftcol) + inImg(uprow, rightcol) +
						inImg(downrow, leftcol) + inImg(downrow, rightcol) );
				}
			}

			outImg(r,c) = sum;
		}
	}

}